package com.tlx.image;

import java.util.ArrayList;
import java.util.List;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.im4java.core.IdentifyCmd;
import org.im4java.process.ArrayListOutputConsumer;
import org.springframework.boot.SpringApplication;

import com.tlx.core.util.TConfig;
import com.tlx.core.util.PrintUtils;

/**
 * 图片压缩工具类
 * 
 * @author zhangwd
 * @version 1.0
 * 
 */
public class ImageBaseUtils {

    public static void main(String[] args) {
        SpringApplication.run(TConfig.class, args);
        PrintUtils.print(getImgInfo("D:/0/a.jpg"));

    }

    // 垂直合并图片，最后一个文件路径就是合并后最终的图片
    public static void joinV(List<String> imageList, String destImg) {

        List<String> convertList = new ArrayList<String>();
        convertList.addAll(imageList);
        convertList.add(destImg);
        String[] arr = (String[]) convertList.toArray(new String[convertList.size()]);

        IMOperation op = new IMOperation();
        op.append();
        // op.p_append();//水平合并
        op.addImage(arr);
        convertRun(op);
    }

    public static int[] getImgInfo(String imagePath) {
        IMOperation op = new IMOperation();
        op.format("%w %h");
        op.addImage(imagePath);
        String wxh = identifyRun(op).get(0);
        String[] wh = wxh.split(" ");
        return new int[] { Integer.parseInt(wh[0]), Integer.parseInt(wh[1]) };
    }

    /**
     * 根据坐标裁剪图片
     * 
     * @param srcPath
     *            要裁剪图片的路径
     * @param newPath
     *            裁剪图片后的路径
     * @param x
     *            起始横坐标
     * @param y
     *            起始挫坐标
     * @param x1
     *            结束横坐标
     * @param y1
     *            结束挫坐标
     */
    public static void cut(String srcPath, String newPath, Integer width, Integer height, Integer x, Integer y) {

        IMOperation op = new IMOperation();
        op.addImage(srcPath);

        /**
         * width：裁剪的宽度 height：裁剪的高度 x：裁剪的横坐标 y：裁剪的挫坐标
         */
        op.crop(width, height, x, y);

        op.addImage(newPath);

        convertRun(op);
    }

    /**
     * 
     * 根据尺寸缩放图片
     * 
     * @param sca
     *            缩放比例：1是基准，表示不缩放
     * @param srcPath
     *            源图片路径
     * @param newPath
     *            缩放后图片的路径
     */
    public static void resizePercent(String srcPath, String newPath, double sca) {
        int width = getImgInfo(srcPath)[0];
        int newWidth = (int) (width * sca);
        resize(srcPath, newPath, newWidth, null);
    }

    /**
     * 
     * 根据尺寸缩放图片
     * 
     * @param width
     *            缩放后的图片宽度，传null表示不用修改，根据另一个值等比缩放
     * @param height
     *            缩放后的图片高度，传null表示不用修改，根据另一个值等比缩放
     * @param srcPath
     *            源图片路径
     * @param newPath
     *            缩放后图片的路径
     */
    public static void resize(String srcPath, String newPath, Integer width, Integer height) {
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        op.resize(width, height);
        op.addImage(newPath);

        convertRun(op);
    }

    // 修改图片质量（quality：0-100）
    public static void quality(String srcPath, String newPath, double quality) {
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        op.quality(quality);
        op.addImage(newPath);

        convertRun(op);
    }

    private static void convertRun(IMOperation op) {
        try {
            ConvertCmd convert = new ConvertCmd();
            convert.setSearchPath(TConfig.getPath("img.ImageMagick"));
            convert.run(op);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static List<String> identifyRun(IMOperation op) {

        try {
            IdentifyCmd identifyCmd = new IdentifyCmd();
            identifyCmd.setSearchPath(TConfig.getPath("img.ImageMagick"));

            ArrayListOutputConsumer output = new ArrayListOutputConsumer();
            identifyCmd.setOutputConsumer(output);

            identifyCmd.run(op);

            return output.getOutput();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
